package cn.mywcc.util;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class Rs4Filter {
    public static Mono<DataBuffer> createData(ServerHttpResponse response, HttpStatus code, String msg){

        response.setStatusCode(code);
        //3.2封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("info",msg);
        map.put("data", new HashMap<String,HttpStatus>(){{put("code",code);}});
        map.put("status",0);
        //3.3作JSON转换
        byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8);

        //3.4调用bufferFactory方法,生成DataBuffer对象
        DataBuffer buffer = response.bufferFactory().wrap(bytes);
        response.getHeaders().add("Content-Type","application/json;charset=utf-8");
        //4.调用Mono中的just方法,返回要写给前端的JSON数据
        return Mono.just(buffer);
    }
}
